package com.ruoyi.project.module.item.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.item.domain.Item;
import com.ruoyi.project.module.item.service.IItemService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 商品 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-08-06
 */
@Controller
@RequestMapping("/module/item")
public class ItemController extends BaseController
{
    private String prefix = "module/item";
	
	@Autowired
	private IItemService itemService;
	
	@RequiresPermissions("module:item:view")
	@GetMapping()
	public String item()
	{
	    return prefix + "/item";
	}
	
	/**
	 * 查询商品列表
	 */
	@RequiresPermissions("module:item:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Item item)
	{
		startPage();
        List<Item> list = itemService.selectItemList(item);
		return getDataTable(list);
	}
	
	/**
	 * 新增商品
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品
	 */
	@RequiresPermissions("module:item:add")
	@Log(title = "商品", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Item item)
	{		
		return toAjax(itemService.insertItem(item));
	}

	/**
	 * 修改商品
	 */
	@GetMapping("/edit/{itemId}")
	public String edit(@PathVariable("itemId") Integer itemId, ModelMap mmap)
	{
		Item item = itemService.selectItemById(itemId);
		mmap.put("item", item);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品
	 */
	@RequiresPermissions("module:item:edit")
	@Log(title = "商品", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Item item)
	{		
		return toAjax(itemService.updateItem(item));
	}
	
	/**
	 * 删除商品
	 */
	@RequiresPermissions("module:item:remove")
	@Log(title = "商品", action = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(itemService.deleteItemByIds(ids));
	}
	
}
